<?php

function showKiwittes($connection, $uid = 0) {
    
    if ($uid > 0)
    {
        $querySel = "SELECT * FROM kiwitte K WHERE uid = '$uid' 
                    OR EXISTS (SELECT * FROM comentario C WHERE uid = '$uid' AND K.kid = C.kid) 
                        ORDER BY K.data DESC;";
    }
    else
    {
        $querySel = "SELECT * FROM kiwitte ORDER BY data DESC;";
    }
    
    $muralResponse = mysql_query($querySel, $connection);

    if ($muralResponse) {
        while ($kiwittes = mysql_fetch_array($muralResponse)) {
            createKiwitte($connection, $kiwittes);
        }
    } else {
        echo "DB mural problems";
    }
}

function addKiwitte($connection, $texto, $userid) {
    $queryKiwitte = "INSERT INTO kiwitte (texto,uid) VALUES ('$texto','$userid');";

    $addResponse = mysql_query($queryKiwitte, $connection);

    if (!$addResponse) {
        echo "Adding kiwitte problems";
    }
}

function addComments($connection, $texto, $userid, $kiwitteid) {
    $queryComment = "INSERT INTO comentario (uid,kid,texto) VALUES ('$userid','$kiwitteid','$texto');";

    $addResponse = mysql_query($queryComment, $connection);

    if (!$addResponse) {
        echo "Adding Comment problems";
    }
}

function createKiwitte($connection, $kiwittes) {
    $queryAuthor = "SELECT login FROM usuario WHERE uid = ";

    $queryCom = "SELECT * FROM comentario WHERE kid = ";

    $openBlock = '<li class="span10">
                    <div class="thumbnail" onmouseover="setKiwitteVisible(' . $kiwittes['kid'] . ')" 
                            onmouseout="setKiwitteUnvisible(' . $kiwittes['kid'] . ')">
                    <div class="page-header">
                    <h2>';

    $commentBlock = '<div id = ' . $kiwittes['kid'] . ' class="commentBlock">
                        <form action="mural.php" method="POST">
                            <textarea name="msgC" style="width: 680px; height: 17px; resize: none" maxlength="512"></textarea>
                            <input style="display: none" type="text" value=' . $kiwittes['kid'] . ' name="idC">
                            <input type="submit" value="Comente" class="btn">
                        </form>
                     </div>';


    $closeBlock = '</div></li>';

    echo $openBlock;    //abrindo um bloco em html para produzir o bloco do kiwitte

    $AuthorKiwitte = mysql_query($queryAuthor . $kiwittes['uid'], $connection);
    $AuthorKiwitte = mysql_fetch_row($AuthorKiwitte);

    //convertendo a data para datetime
    $dt = new DateTime($kiwittes['data']);


    echo $AuthorKiwitte[0] . '</h2>';
    echo '<p>' . $kiwittes['texto'] . '</p><small class="pull-right">' . $dt->format('j M y - G:i') . '</small></div>';

    $cmtResponse = mysql_query($queryCom . $kiwittes['kid'], $connection);

    if ($cmtResponse) {

        echo "<ul class='comments'>";
        while ($comments = mysql_fetch_array($cmtResponse)) {
            $AuthorCom = mysql_query($queryAuthor . $comments['uid'], $connection);
            $AuthorCom = mysql_fetch_row($AuthorCom);

            echo "<li><blockquote>";
            echo $comments['texto'];
            echo "<small>" . $AuthorCom[0] . "</small>";
            echo "</blockquote></li>";
        }
        echo "</ul>";
    } else {
        echo "DB comentarios problems";
    }

    echo $commentBlock;
    echo $closeBlock;   //Fechando o bloco de html
}

?>